package myPetStore.web.servlet;

import myPetStore.domain.*;
import myPetStore.persistence.LogDao;
import myPetStore.persistence.impl.LineItemDaoImpl;
import myPetStore.persistence.impl.LogDaoImpl;
import myPetStore.persistence.impl.OrderDaoImpl;
import myPetStore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

public class ConfirmOrderServlet extends HttpServlet {
    private OrderDaoImpl orderDao;
    private LineItemDaoImpl lineItemDao;
    private static final String  VIEW_ORDER="/WEB-INF/jsp/order/viewOrder.jsp";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        Cart cart=(Cart)session.getAttribute("cart");
        Order order=(Order)session.getAttribute("order");
        orderDao=new OrderDaoImpl();
        orderDao.insertOrder(order);
        for(int i = 0; i < order.getLineItems().size(); i++) {
            lineItemDao=new LineItemDaoImpl();
            LineItem lineItem=new LineItem();
            lineItem = order.getLineItems().get(i);
            lineItem.setOrderId(order.getOrderId());
            this.lineItemDao.insertLineItem(lineItem);
        }

        Account loginAccount = (Account)session.getAttribute("loginAccount");
        Log log = new Log();
        log.setLogTime(new Date());
        log.setUserName(loginAccount.getUsername());
        log.setTitle("订单信息");
        log.setContent("用户" + loginAccount.getUsername() + "新增一个订单");
        LogDao logDao = new LogDaoImpl();
        logDao.InsertLog(log);

        cart.clearCart();
        req.getRequestDispatcher(VIEW_ORDER).forward(req,resp);
    }
}
